Flash Article

Data for Designers: Connecting to Data in Flash Using Data Wizards


Table of Contents

  1. Introduction
  2. Getting the Required Software
  3. Understanding the Data Connection Wizard
  4. Syndicating XML with the Data Connection Wizard
  5. Using the DataGrid Column Editor
  6. Consuming Web Services Using Wizards

Syndicating XML with the Data Connection Wizard

Before we go too far, let's look at a simple example of XML feed syndication. If you work with XML data, you should check out some of the issues and good habits to follow when working with XML and the Data Connection wizard:

  • White space within the XML packets is ignored by default.
  • The XML must return an array for the wizard to work effectively.
  • If you get errors while working with XML, view the XML file in a web browser and check that the XML packet doesn't contain errors. If the XML has errors, the wizard will not work.
  • If you specify a relative path to an XML file, it might be necessary to save the Flash document before testing the movie so that Flash knows where to find the XML file.

The very first project I tried after downloading the new components syndicated my blog's XML file. If you're one of the two dozen people on the Internet without your own blog (what are you waiting for?), you can use the Movable Type news feed for testing at www.movabletype.org/news/index.rdf.

The easiest way to syndicate the Movable Type news feed is to open a new Flash document in Flash MX Professional 2004 and then choose Commands > Data Connection Wizard. If you have a slower computer, launching the wizard might take a few seconds. The Data Connection wizard has four major steps that make it easy to connect external data sources to your SWF file. Let's go through each of these steps now.

Step 1: Choose a Data Source

After launching the Data Connection wizard (choose Commands > Data Connection Wizard), you have the choice of using one of two different data sources:

  • XML is data structured in a flexible yet consistent way. This information might be on the same or different server than your SWF.
  • Web services is data provided by a web server for use by other users or applications either on the web or on your desktop and connected to the web. You can also use custom or private web services on the same server.
  1. Select XML from the Type of Data Source drop-down menu. Flash will prompt you to type in a URL or file path to the XML document (like the movabletype.org address earlier).

    Note: Unfortunately, it seems like you are unable to paste a value into the Document URL text field using Control-V. However, if you give focus to the text field by clicking in it, and then right-click or Control-click the Document URL field, you can then paste the URL. Also remember that you must prefix URLs with http:// or else you will get an error.

  2. Type in a remote document URL to an XML file in the Document URL field.
  3. Make sure the From The Document URL radio button is selected.
  4. Click the Get Schema button to retrieve and display the XML file's schema in the list box. Here you choose the items you want to display in the resulting DataGrid.

    Note: The schema is the organization and structure of the XML file. Getting and viewing schema is useful because you can choose what data to bind. The schema is also a visual representation of the data you are bringing into Flash.

  5. After the schema has been loaded into the wizard, expand the RDF:Object entry and click the item:Array entry. Notice that the item Array has a red arrow, which means it can be used by the wizard. Red arrows can be used by the wizard, while black arrows cannot (the information might be in a nested array). Also notice that the XPath text field automatically populates when you click the appropriate entries (see Figure 2).
  6. XML arrives in packets, which is the data that you use in Flash. If you want to view the structure of the XML packet, expand the item array by clicking the plus (+) icon next to the arrow.
  7. When you are finished, make sure item:Array is still selected and click the Next button to continue with the wizard.
Using the schema in the Data Connection wizard

Figure 2. Using the schema in the Data Connection wizard

Step 2: Choose Data Fields

The next step in the wizard allows you to select columns to display in the DataGrid component you're creating. You can choose to display all or none of the columns by using the buttons below the data field's list box component. Alternatively, choose particular columns by clicking your selections in this window:

  1. Click to display both the title:String and creator:String data fields (see Figure 3). The List component doesn't allow you to click two rows and keep them both selected. In order to select two rows simultaneously, Control-click (or Command-click) each row separately.
  2. Click Next.
Selecting data fields to work with in Flash

Figure 3. Selecting data fields to work with in Flash

Step 3: Set Up Data Fields

For this simple example you can click Next and skip this step because you only need the two fields of data (title and creator) that are already selected. However, if you need to define additional fields, Step 3 in the wizard lets you add or remove fields, as well as define a data type (values that have defined characteristics) for each field. For example, use this step if you are using ColdFusion and need to define the fields manually. XML data with XPath but without an XML data source also need the fields defined here. Do this if the schema doesn't import into the wizard.

Step 4: Choose Components

The final step of the wizard allows you to choose a prefix for the generated components:

  1. In the Generated Components label enter rdf. This means this string is prefixed to the instance name for the XML (or WebService) connector, the DataSet, and the DataGrid.
  2. Make sure both the Create a DataGrid and Create "Get Data" Button check boxes are both selected so the DataGrid component and Get Data button are added to the Stage (see Figure 4).
  3. Click Finish to complete the wizard and behold the magic.
Labeling components and choosing which ones are generated

Figure 4. Labeling components and choosing which ones are generated

Looking at the Final Product

If this is the first time you have employed the services of the XMLConnector or DataSet component (components from Flash MX Professional that the wizard adds to your document), you might be interested to know that even though the component icons are located on the Stage, they don't appear within the SWF file at runtime. Therefore, it is safe to place the components anywhere you want around the Stage, or even completely off the Stage if you prefer.

Notice that Flash places four components on the Stage:

  • XMLConnector (with an instance name of rdfReceiveXMLConnector)
  • DataSet (instance name rdfDataSet)
  • DataGrid (instance name rdfDataGrid)
  • Button (no instance name)

Test the movie by pressing Control+Enter (Command+Return on the Mac). The XML file doesn't load into the DataGrid until you click the Get Data button on the Stage. Now you can read the news headlines. The author's name after the DataGrid populates from the contents of the XML file. Resize the column widths by dragging the vertical line between the Title and Creator columns.

VoilĂ ! You managed to syndicate content from an external source and bind the results to a DataGrid without writing a single line of ActionScript. No, you don't have to be a developer to use this stuff.

Note: One of the best parts of the Data Connection wizard is the fact that help is always accessible throughout the process. On the left side of the wizard, the context-sensitive Questions area helps you look up answers to frequently asked questions. Additionally, you can click the Help button near the bottom-left corner of the Data Connection wizard at any point to learn how to use the wizard.

If you find it annoying always having to click Get Data on the Stage to load the data, don't fret. You can delete the Button instance on the Stage and add a Flash behavior (from the Behaviors panel) that loads the data automatically. You still don't have to write any ActionScript to accomplish this impressive feat:

  1. Add a new layer and select Frame 1 on the new layer.
  2. Open the Behaviors panel.
  3. In the Behaviors panel, click the Add Behavior button (+) and select Data > Trigger Data Source from the menu.
  4. In the resulting Trigger Data Source window, select the XMLConnector component (it should have the instance name rdfReceiveXMLConnector)
  5. Click OK to add the behavior to the document.

Now when you test the movie, you won't have to click Get Data to load the XML. The data automatically loads when the SWF file runs because it triggers in the first frame. You can now delete that pesky Get Data button from the Stage.